package cliente;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.Scanner;

import at.jta.Key;
import at.jta.RegistryErrorException;
import at.jta.Regor;

public class Identificador {
	
	private  String ID_Placa(){
		  try {
		 	Process process = Runtime.getRuntime().exec(new String[] { "wmic", "BaseBoard", "get", "serialnumber" });
	      	process.getOutputStream().close();
		    Scanner sc = new Scanner(process.getInputStream());
		    sc.next();
	        return sc.next();
	        } catch (Exception e) {
				return "";
			}
	}
	 private   String ID_DiscoDuro() {
		  String result = "";
		    try {
		      File file = File.createTempFile("realhowto",".vbs");
		      file.deleteOnExit();
		      FileWriter fw = new java.io.FileWriter(file);
		      String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
		                  +"Set colDrives = objFSO.Drives\n"
		                  +"Set objDrive = colDrives.item(\"" + "C" + "\")\n"
		                  +"Wscript.Echo objDrive.SerialNumber";
		      fw.write(vbs);
		      fw.close();
		      Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
		      BufferedReader input =
		        new BufferedReader
		          (new InputStreamReader(p.getInputStream()));
		      String line;
		      while ((line = input.readLine()) != null) {
		         result += line;
		      }
		      input.close();
		    }
		    catch(Exception e){
		        return "";
		    }
		    return result.trim();
		  }
	 public boolean existeLLave(String software) throws RegistryErrorException{
		 	Regor reg = null;
	        String llave = "Software\\" + software;
	 	    reg = new Regor();
	        if(reg.openKey(Regor.HKEY_CURRENT_USER, llave) == null) 
	        	return false;
	        else
	        	return true;
		 
	 }
	 public boolean registrar(String software,String clave) throws RegistryErrorException  {
	        try {
				Regor reg = null;
				Key key = null;
				String llave = "Software\\" + software;
				reg = new Regor();
				reg.createKey(Regor.HKEY_CURRENT_USER, llave);
				key = reg.openKey(Regor.HKEY_CURRENT_USER, llave);
				reg.saveValue(key, "KeyActivation", clave);
				return true;
			} catch (Exception e) {
				return false;
			}
	            //reg.saveDword(key, "Count", "1");
	        
       
	 
	        //delete an entry any type
	      // reg.deleteEntry(key, "KeyActivation");
	 
	        //delete a subkey
	    //   reg.delKey(Regor.HKEY_CURRENT_USER, SBK);
	}
	 public String capturarIdMaquina() throws RegistryErrorException{
		 String idTarjeta = ID_Placa();
		 String idDisco = ID_DiscoDuro();
		 return idTarjeta+idDisco;
	 }
	
}

